﻿using SAIM.Models.BLL;
using SAIM.Models.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace SAIM.Models
{

    public class ListsViewModel
    {
        [Required(AllowEmptyStrings=false, ErrorMessage="Nombre de lista obligatorio")]
        [Display(Name = "Nombre")]
        public string Id { get; set; }

        public string RealName { get; set; }

        /// <summary>
        /// Indica si esta lista será eliminada
        /// </summary>
        public bool Delete { get; set; }

        /// <summary>
        /// Contiene a todos los usuarios que pueden ver esta lista
        /// </summary>
        public List<UsersLists> Users { get; set; }

        /// <summary>
        /// Identificador (UPN) del usuario creador de la lista
        /// </summary>
        public string PrimaryUser { get; set; }

        /// <summary>
        /// Contiene un conjuto de nombres de listas personalizadas 
        /// </summary>
        public Dictionary<string, string> UserLists { get; set; }

        /// <summary>
        /// Contiene la información del usuario actual (Solicitante)
        /// Permite la configuración de las acciones que un usuario puede realizar
        /// </summary>
        public UsersLists ActualUser { get; set; }

        /// <summary>
        /// Contiene los nombres de las columnas que conforman a esta lista
        /// </summary>
        public List<string> ListStructure { get; set; }

        public ListsViewModel()
        {
            Id = string.Empty;
            UserLists = new Dictionary<string, string>();
            Users = new List<UsersLists>();
            PrimaryUser = string.Empty;
            ListStructure = new List<string>();
        }

        /// <summary>
        /// Crea una nueva instancia de esta clase mediante un conjunto de nombres de listas personalizadas 
        /// </summary>
        /// <param name="listsNames"></param>
        public ListsViewModel(Dictionary<string, string> lists) 
        {
            UserLists = lists;
            Id = string.Empty;
            Users = new List<UsersLists>();
        }
    }

    public class UsersLists
    {
        public string UserUPN { get; set; }
        public string UserName { get; set; }
        public bool DDLPermissions { get; set; }
        public bool DMLPermissions { get; set; }
        public bool Delete { get; set; }
    }

    public class ColumnData
    {
        [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(SAIM.Resources.Validations), ErrorMessageResourceName = "FieldRequired")]
        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "Name")]
        public string ListId { get; set; }

        public string DataType { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "Description")]
        public string Description { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(SAIM.Resources.Validations), ErrorMessageResourceName = "ColumnNameRequired")]
        [RegularExpression(@"^[a-zA-Z0-9\sñÑáéíóúÁÉÍÓÚ]*$", ErrorMessageResourceType = typeof(SAIM.Resources.Validations), ErrorMessageResourceName = "AlphanumericValuesOnly")]
        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "ColumnName")]
        public string ColumnName { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "FieldRequired")]
        public bool Required { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "ForceUniqueValues")]
        public bool Unique { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "MaxNumChar")]
        public int? Size { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "DefaultValue")]
        public string DefaultValue { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "TypeChoices")]
        public string Choices { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "DisplayChoicesUsing")]
        public string ChoiceType { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "MinValue")]
        public long? MinSize { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "MaxValue")]
        public long? MaxSize { get; set; }

        [Display(ResourceType = typeof(SAIM.Resources.Site), Name = "DecimalPlaces")]
        public int? DecimalPlaces { get; set; }

        /// <summary>
        /// Indica si esta columna será eliminada
        /// </summary>
        public bool DeleteColumn { get; set; }

        public ColumnData()
        {
            Required = false;
            Unique = false;
            DeleteColumn = false;
        }

        public void SetDefaults()
        {
            if (DataType == "Text")
                Size = Size ?? 255;
            if (DataType == "Decimal")
                DecimalPlaces = DecimalPlaces ?? 3;
        }
    }

    public class AddItemViewModel
    {
        public List<ListStructure> ColumnsList;
        public string ListId;

        public AddItemViewModel(string listId, List<ListStructure> structure)
        {
            ColumnsList = structure;
            ListId = listId;
        }
    }

    public class EditItemViewModel
    {
        public List<ListStructure> ColumnsList;
        public string ListId;
        public Dictionary<string, object> Data;
        public bool Delete { get; set; }

        public EditItemViewModel(string listId, List<ListStructure> structure,  Dictionary<string, object> data)
        {
            ColumnsList = structure;
            ListId = listId;
            Data = data;
        }
    }

    public class GetListDataViewModel
    {
        public int current { get; set; }
        public int rowCount { get; set; }
        public int total { get; set; }
        public List<Dictionary<string, object>> rows;

        public GetListDataViewModel(List<Dictionary<string, object>> _rows, int _current = 1, int _rowCount = 10)
        {
            current = _current;
            rowCount = _rowCount;
            total = _rows.Count;
            rows = _rows;
        }
    }

    public class GetListAttachmentsViewModel
    {
        public int current { get; set; }
        public int rowCount { get; set; }
        public int total { get; set; }
        public List<ListAttachment> rows;

        public GetListAttachmentsViewModel(int _current = 1, int _rowCount = 10)
        {
            current = _current;
            rowCount = _rowCount;
            rows = new List<ListAttachment>();
        }
    }

    public class ListAttachment
    {
        public string listId { get; set; }
        public string FileName { get; set; }
        public string Mask { get; set; }
        public string FileSize { get; set; }
        public string CreationDate { get; set; }
        public string UserName { get; set; }
        public bool HasEditions { get; set; }
    }
}